package com.bawei.yangzongcai___chenmei.Service;

import android.animation.ObjectAnimator;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.view.animation.BounceInterpolator;
import android.widget.ImageView;
import android.widget.RemoteViews;
import android.widget.SeekBar;

import com.bawei.yangzongcai___chenmei.R;

import java.io.IOException;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class MyService extends Service {

    MediaPlayer mediaPlayer = new MediaPlayer();
    int daodan=0;
    ObjectAnimator rotationObjectAnimator;
    public void dongfeng(int position) {
        tongzhService();
        daodan=position;
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource(MyServiceActivity.classList.get(position).getData());
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaPlayer.prepareAsync();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mediaPlayer.start();
                if (mediaPlayer.isPlaying()){
                    //风火轮旋转
                    rotationObjectAnimator = ObjectAnimator.ofFloat(MyServiceActivity.verhander.imageView,"rotation",0,360,0,360);
                    rotationObjectAnimator.setDuration(10000*5);
                    rotationObjectAnimator.start();
                }else {
                    rotationObjectAnimator.clone();
                }
                MyServiceActivity.mataSeek.setMax(mediaPlayer.getDuration());

            }
        });

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                MyServiceActivity.mataSeek.setProgress(mediaPlayer.getCurrentPosition());
            }
        },0,1000);
    }


    public void bofang() {
        mediaPlayer.start();
    }

    public void zanting() {
        mediaPlayer.pause();
    }

    public void shangyiqu() {
        switch (MyServiceActivity.haizi)
        {
            case 1:
                dongfeng(daodan);
                break;
            case 2:
                if (--daodan<0)//顺序播放
                {
                    daodan=MyServiceActivity.classList.size()-1;
                }
                dongfeng(daodan);
                break;


            case 3:
                Random random = new Random();
                daodan = random.nextInt(MyServiceActivity.classList.size());
                dongfeng(daodan);
                break;
        }
    }

    public void xiayiqu() {
        switch (MyServiceActivity.haizi)
        {
            case 1:
                dongfeng(daodan);
                break;
            case 2:
                if (++daodan>MyServiceActivity.classList.size()-1)//顺序播放
                {
                    daodan=0;
                }
                dongfeng(daodan);
                break;


            case 3:
                Random random = new Random();
                daodan = random.nextInt(MyServiceActivity.classList.size());
                dongfeng(daodan);
                break;
        }
    }

    public MyService() {

    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Myguangbo myService = new Myguangbo();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("yihao");
        intentFilter.addAction("erhao");
        intentFilter.addAction("sanhao");
        intentFilter.addAction("sihao");
        registerReceiver(myService,intentFilter);

        MyServiceActivity.mataSeek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser)
                {
                    mediaPlayer.seekTo(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                switch (MyServiceActivity.haizi)
                {
                    case 1:
                        dongfeng(daodan);
                        break;
                    case 2:
                        if (++daodan>MyServiceActivity.classList.size()-1)//顺序播放
                        {
                            daodan=0;
                        }
                        dongfeng(daodan);
                        break;


                    case 3:
                        Random random = new Random();
                        daodan = random.nextInt(MyServiceActivity.classList.size());
                        dongfeng(daodan);
                        break;
                }
            }
        });
        return super.onStartCommand(intent, flags, startId);
    }


    public class MyBanker extends Binder
    {
        public MyService servicelist()
        {
            return MyService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {

        return new MyBanker();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    //通知前台服务
    public void tongzhService() {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notificationservice);
        builder.setCustomContentView(remoteViews);
        //点击更新图片歌曲歌名子
        Bitmap bitmap = BitmapFactory.decodeFile( MyServiceActivity.classList.get(daodan).getAlbum_id());
        remoteViews.setImageViewBitmap(R.id.zhale_img,bitmap);
        remoteViews.setTextViewText(R.id.zhale_geshou,""+MyServiceActivity.classList.get(daodan).getArtist());
        remoteViews.setTextViewText(R.id.zhale_gename,""+MyServiceActivity.classList.get(daodan).getTitle());

        Intent intent = new Intent();
        intent.setAction("yihao");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,intent,0);
        remoteViews.setOnClickPendingIntent(R.id.zhale_shangyiqu,pendingIntent);

        Intent intent2 = new Intent();
        intent2.setAction("erhao");
        PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this,0,intent2,0);
        remoteViews.setOnClickPendingIntent(R.id.zhale_bofang,pendingIntent2);

        Intent intent3 = new Intent();
        intent3.setAction("sanhao");
        PendingIntent pendingIntent3 = PendingIntent.getBroadcast(this,0,intent3,0);
        remoteViews.setOnClickPendingIntent(R.id.zhale_zanting,pendingIntent3);

        Intent intent4 = new Intent();
        intent4.setAction("sihao");
        PendingIntent pendingIntent4 = PendingIntent.getBroadcast(this,0,intent4,0);
        remoteViews.setOnClickPendingIntent(R.id.zhale_xiayiqu,pendingIntent4);

//        //前台服务
        startForeground(1,builder.build());
    }


    //广播判断
    public class Myguangbo extends BroadcastReceiver
    {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("yihao"))
            {
                shangyiqu();
            }
            if (intent.getAction().equals("erhao"))
            {
               bofang();
            }
            if (intent.getAction().equals("sanhao"))
            {
               zanting();
            }
            if (intent.getAction().equals("sihao"))
            {
                xiayiqu();
            }

        }
    }

}
